package server.business.controller;//package server.business.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.controller.dto.Discardapply;
import server.business.pojo.BloodDiscardApply;
import server.business.pojo.BloodDiscardReview;
import server.business.service.BloodDiscardReviewService;
import server.business.service.BloodInformationService;
import server.business.service.BloodDiscardApplyService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author wwjh
 * @date 2022年08月05日 10:58
 */
@Slf4j
@RestController
@RequestMapping("/discard")
public class BloodDiscardApplyMapperController {
    @Autowired
    private BloodDiscardApplyService bloodDiscardApplyService;
    @Autowired
    private BloodInformationService bloodInformationService;

    @Autowired
    private BloodDiscardReviewService bloodDiscardReviewService;

    @PostMapping("/normal/Data")
    @ResponseBody
    public boolean requestdata33(@RequestBody Discardapply discardapply){
        if (StrUtil.isBlank(discardapply.getApplicant())||StrUtil.isBlank(discardapply.getApplicantId())||StrUtil.isBlank(discardapply.getScrapReason())){
            return false;
        }else{
            log.info("-------------------------------");
            BloodDiscardApply bloodDiscardApply =new BloodDiscardApply();
            String discardapplicantid = discardapply.getApplicantId();
            GenerateNumberBySnowFlake discardApplyId =new GenerateNumberBySnowFlake(discardapplicantid,"BF");
            bloodDiscardApply.setApplyId(discardApplyId.genNumberByflag());
            bloodDiscardApply.setBloodId(discardapply.getBloodId());
            bloodDiscardApply.setScrapReason(discardapply.getScrapReason());
            bloodDiscardApply.setBarcode(Long.valueOf(discardapply.getBarcode()));
            bloodDiscardApply.setBloodSource(discardapply.getBloodSource());
            bloodDiscardApply.setAboBloodType(discardapply.getAboBloodType());
            bloodDiscardApply.setRhBloodType(discardapply.getRhBloodType());
            bloodDiscardApply.setBloodComponent(discardapply.getBloodComponent());
            bloodDiscardApply.setMeasurementUnit(Integer.valueOf(discardapply.getMeasurementUnit()));
            bloodDiscardApply.setBloodAmount(Long.valueOf(discardapply.getBloodAmount()));
            bloodDiscardApply.setBloodCount(Long.valueOf((discardapply.getBloodCount())));
            bloodDiscardApply.setIsDeleted(0);
            bloodInformationService.setStateByBloodNumber(discardapply.getBloodId(),"4");
            bloodDiscardApplyService.saveOrUpdate(bloodDiscardApply);
            //上面是保存到apply表里面
            //下面是保存在审核discardreview表里面
            BloodDiscardReview bloodDiscardReview = new BloodDiscardReview();
            bloodDiscardReview.setBloodDiscardId(bloodDiscardApply.getApplyId());
            bloodDiscardReview.setApplicantId(discardapply.getApplicantId());
            Date day=new Date();
            bloodDiscardReview.setApplyTime(day);
            bloodDiscardReview.setIsDeleted(0);
            bloodDiscardReview.setState("01");
            bloodDiscardReviewService.saveOrUpdate(bloodDiscardReview);
            return true;
        }

    }
    @GetMapping("/normal/page")
    @ResponseBody
    public IPage<BloodDiscardApply> findPage1(HttpServletRequest request, HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        IPage<BloodDiscardApply> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodDiscardApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        //总查询
        return bloodDiscardApplyService.page(page,queryWrapper);
    }
    // 新增和修改
    @PutMapping("/normal/simpleupdate")
    public boolean save(@RequestBody BloodDiscardApply bloodDiscardApply) {
        // 新增或者更新
        log.info(String.valueOf(bloodDiscardApply));
        return bloodDiscardApplyService.updateById(bloodDiscardApply);
    }
//    @PostMapping("/audit")
//    public boolean save1(@RequestBody BloodDiscard bloodDiscard) {
//        // 新增或者更新
//
//        bloodDiscardService.setStateByBloodId(bloodDiscard.getBloodId());
//        log.info(String.valueOf(bloodDiscard));
//        return bloodDiscardService.updateById(bloodDiscard);
//    }
//    @PostMapping("/dengji")
//    public boolean save12(@RequestBody BloodDiscard bloodDiscard) {
//        // 新增或者更新
//        log.info(String.valueOf(bloodDiscard));
//        return bloodDiscardService.updateById(bloodDiscard);
//    }
    @DeleteMapping("/normal/{id}")
    public boolean delete(@PathVariable Integer id) {
        log.info("-------------------------");
        log.info(String.valueOf(id));
        log.info(id.getClass().toString());
        String DiscardId = bloodDiscardApplyService.findSampleDiscardApplyidById(id);
        String BloodId = bloodDiscardApplyService.findSampleBloodIdById(id);
        bloodInformationService.setStateByBloodNumber(BloodId,"2");
        bloodDiscardReviewService.setisDeletedById(DiscardId,1);
        return bloodDiscardApplyService.removeById(Long.valueOf(id));
    }

    @GetMapping("/normal/querypage")
    @ResponseBody
    public IPage<BloodDiscardApply> findPage(HttpServletRequest request,HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String applyId1= request.getParameter("applyId1");
        String bloodId = request.getParameter("bloodId");
        String scrap_reason = request.getParameter("scrap_reason");
        IPage<BloodDiscardApply> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodDiscardApply> queryWrapper = new QueryWrapper<>();
        //如果id被填写
        if (applyId1 != ""&& applyId1!= null) {
            queryWrapper.like("apply_id",applyId1);
        }
        if (bloodId != ""&& bloodId!= null){
            queryWrapper.eq("blood_id",bloodId);
        }
        if(scrap_reason!=""&& scrap_reason!= null){
            queryWrapper.eq("scrap_reason",scrap_reason);
        }
        queryWrapper.eq("is_deleted",0);
        return bloodDiscardApplyService.page(page,queryWrapper);
    }
//    @GetMapping("/querypage123")
//    @ResponseBody
//    public IPage<BloodDiscard> findPage123(HttpServletRequest request,HttpServletResponse response){
//        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
//        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
//        String id= request.getParameter("id");
//        String startTime = request.getParameter("startTime");
//        String endTime = request.getParameter("endTime");
//        String number = request.getParameter("number");
//        String scrap_reason = request.getParameter("scrap_reason");
//        String auditor = request.getParameter("auditor");
//        IPage<BloodDiscard> page = new Page<>(pageNum,pageSize);
//        QueryWrapper<BloodDiscard> queryWrapper = new QueryWrapper<>();
//        //如果id被填写
//        if (id != ""&& id!= null) {
//            queryWrapper.like("id",id);
//        }
//        if (number != ""&& number!= null){
//            queryWrapper.eq("number",number);
//        }
//        if(scrap_reason!=""&& scrap_reason!= null){
//            queryWrapper.eq("scrap_reason",scrap_reason);
//        }
//        if(auditor!=""&& auditor!= null){
//            queryWrapper.eq("auditor",auditor);
//        }
//        if (startTime !=""&& startTime!= null){
//            queryWrapper.ge("apply_time",startTime);
//        }
//        if (endTime != ""&& endTime!= null){
//            queryWrapper.le("apply_time",endTime);
//        }
//        return bloodDiscardService.page(page,queryWrapper);
//
//    }
        @RequestMapping("/normal/batchDelete")
        @ResponseBody
        public Result batchDelete(@RequestBody ArrayList<Long> ids) {
            System.out.println(ids);
            //获取批量删除血液Id列表
            ArrayList<String> bloodIdList = bloodDiscardApplyService.findBloodIdList(ids);
            for(int i=0;i<bloodIdList.size();i++){
                //循环调用赋值血液基本信息状态
                bloodInformationService.setStateByBloodNumber(bloodIdList.get(i),"2");
            }
            //获取批量删除申请单号列表
            ArrayList<String> ApplyidList = bloodDiscardApplyService.findDiscardApplyidById(ids);
            for(int j=0;j<bloodIdList.size();j++){
                bloodDiscardReviewService.setisDeletedById(ApplyidList.get(j),1);
            }
            boolean success = bloodDiscardApplyService.removeByIds(ids);
            Result result = new Result();
            if (success) {
                result.setCode("200");
                result.setMsg("删除成功");
            } else {
                result.setCode("500");
                result.setMsg("删除失败");
            }
            return result;
        }

}
